package org.leiyang.common.dtos;

import lombok.Getter;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;

/**
 * {@link Message} 类型
 */
@Getter
public enum MessageType {

    TOKEN(1),
    MSG_TEXT(2),
    MSG_VIDEO(3),
    MSG_AUDIO(4),
    MSG_IMG(5),
    PONG(6),
    EMPTY(-1);

    private final int type;

    MessageType(int type) {
        this.type = type;
    }

    public static MessageType get(int value) {
        AtomicReference<MessageType> result = new AtomicReference<>(MessageType.EMPTY);
        Arrays.stream(MessageType.values())
                .filter(messageType -> messageType.type == value)
                .findAny()
                .ifPresent(messageType -> result.compareAndSet(MessageType.EMPTY, messageType));
        return result.get();
    }
}